package com.xiaotu.spider.qq.pipeline;

import com.xiaotu.common.model.SgWxModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.DataRedisKeyUtils;
import com.xiaotu.common.util.FileUtil;
import com.xiaotu.spider.APipeline;
import com.xiaotu.spider.qq.pageprocessor.TVSgWxPageProcessor;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;

import java.io.File;

/**
 * Description
 *
 * @auther GaoHJ
 * @create 2017/10/26
 */
@Component("SgWxPipeline")
public class SgWxPipeline extends APipeline
{
    private static final String FILE_NAME = "%s_%s_%s.html";

    @Override
    protected void doProcess(ResultItems resultItems, Task task)
    {
        SgWxModel wx = resultItems.get(SgWxModel.class.getName());
        if (wx == null)
            return;
        String flag = DataRedisKeyUtils.getSgWxKey(wx);
        if (cacheHandler.hExists(Constants.DataRedisKey.Sougo_WX_History.toString(), flag))
            return;
        wx.setFileName(String.format(FILE_NAME, wx.getTargetId(), wx.getTargetType(), System
                .currentTimeMillis()));
        String key = DataRedisKeyUtils.getKey(Constants.DataRedisKey.Sougo_WX_YYYYMMDD);
        JSONObject jsonObj = JSONObject.fromObject(wx);
        jsonObj.put(Constants.TargetType.TV.equals(wx.getTargetType()) ? "tvId" : "starId", wx
                .getTargetId());
        cacheHandler.hSet(key, flag, jsonObj);
        cacheHandler.setCacheTimeout(key);
        cacheHandler.hSet(Constants.DataRedisKey.Sougo_WX_History.toString(), flag, StringUtils
                .EMPTY);

        FileUtil.writeFile(resultItems.get(TVSgWxPageProcessor.PATH_PARAM) + File.separator + wx
                .getFileName(), resultItems.get("html"));
    }
}
